/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.evaluationSystem.controller;

import com.evaluationSystem.annotation.LogOperation;
import com.evaluationSystem.config.R;
import com.evaluationSystem.entity.dto.SysRoleDTO;
import com.evaluationSystem.service.SysRoleDataScopeService;
import com.evaluationSystem.service.SysRoleMenuService;
import com.evaluationSystem.service.SysRoleService;
import com.evaluationSystem.util.AssertUtils;
import com.evaluationSystem.util.PageData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 * 
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysRoleDataScopeService sysRoleDataScopeService;

	@GetMapping("page")
	@RequiresPermissions("sys:role:page")
	public R<PageData<SysRoleDTO>> page(@RequestParam Map<String, Object> params){
		PageData<SysRoleDTO> page = sysRoleService.page(params);

		return R.success(page);
	}

	@GetMapping("list")
	@RequiresPermissions("sys:role:list")
	public R<List<SysRoleDTO>> list(){
		List<SysRoleDTO> data = sysRoleService.list(new HashMap<>(1));

		return R.success(data);
	}

	@GetMapping("{id}")
	@RequiresPermissions("sys:role:info")
	public R<SysRoleDTO> get(@PathVariable("id") Long id){
		SysRoleDTO data = sysRoleService.get(id);

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.getMenuIdList(id);
		data.setMenuIdList(menuIdList);

		//查询角色对应的数据权限
		List<Long> deptIdList = sysRoleDataScopeService.getDeptIdList(id);
		data.setDeptIdList(deptIdList);

		return R.success(data);
	}

	@PostMapping
	@LogOperation("保存")
	@RequiresPermissions("sys:role:save")
	public R save(@RequestBody SysRoleDTO dto){

		sysRoleService.save(dto);

		return R.success();
	}

	@PutMapping
	@LogOperation("修改")
	@RequiresPermissions("sys:role:update")
	public R update(@RequestBody SysRoleDTO dto){

		sysRoleService.update(dto);

		return R.success();
	}

	@DeleteMapping
	@LogOperation("删除")
	@RequiresPermissions("sys:role:delete")
	public R delete(@RequestBody Long[] ids){
		//效验数据
		AssertUtils.isArrayEmpty(ids, "id");

		sysRoleService.delete(ids);

		return R.success();
	}
}